//
//  CPShareDetailController.m
//  ChinaPoem
//
//  Created by 赵俊明 on 2019/3/7.
//  Copyright © 2019 赵俊明. All rights reserved.
//

#import "CPShareDetailController.h"
#import "CPLabel.h"
#import "NSString+CPString.h"
#import "CPShareItem.h"

@interface CPShareDetailController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property(nonatomic, weak) UIView *bkg;
@property(nonatomic, weak) UIImageView *bkgImage;
@property(nonatomic, strong) NSMutableArray *dataSource;
@end

@implementation CPShareDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.RImage = @"share";
    self.title = _model.cp_title;
    [self cp_setupLabel];
    
    JMSelf(ws);
    CPShareItem *shaItem = [[CPShareItem alloc] initWithFrame:CGRectZero];
    [self.view addSubview:shaItem];
    shaItem.DidSelect = ^(NSString * _Nonnull imaName, BOOL isAdd) {
        if (isAdd) {
            [self cp_setupImagePicker:0];
        }else{
            ws.bkgImage.image = [UIImage imageNamed:imaName];
            ws.bkgImage.hidden = NO;
        }
    };
    [shaItem mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(ws.bkg.mas_bottom).offset(10);
        if (@available(iOS 11.0, *)) {
            make.bottom.mas_equalTo(self.view.mas_safeAreaLayoutGuideBottom).offset(-10);
        } else {
            make.bottom.mas_equalTo(self.view.bottom).offset(-10);
        }
        make.width.mas_equalTo(self.view);
    }];
}

- (void)cp_setupImagePicker:(NSInteger)number
{
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.view.frame = CGRectMake(0, 0, self.view.width, self.view.height - 64);
    picker.delegate = self;
    picker.allowsEditing = YES;
    [self presentViewController:picker animated:YES completion:nil];
}

#pragma mark -- UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey, id> *)info
{
    _bkgImage.image = info[UIImagePickerControllerEditedImage];
    _bkgImage.hidden = NO;
    [picker dismissViewControllerAnimated:YES completion:nil];
}

// 添加代码，处理选中图像又取消的情况
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker dismissViewControllerAnimated:YES completion:nil];
}

- (void)cp_rightBtn
{
    UIImage *image = [self.bkg snapshotImage];
    [self cp_share:nil image:image handle:^(UIActivityType  _Nullable activityType, BOOL completed) {}];
}

#pragma mark - **************** cp_setupLabel
- (void)cp_setupLabel
{
    UIView *bkg = [[UIView alloc] initWithFrame:CGRectMake(0, 64, self.view.width, kH-140)];
    [self.view addSubview:bkg];
    bkg.backgroundColor = [UIColor redColor];
    self.bkg = bkg;
    [bkg mas_makeConstraints:^(MASConstraintMaker *make) {
        if (@available(iOS 11.0, *)) {
            make.top.mas_equalTo(self.view.mas_safeAreaLayoutGuideTop).offset(3);
        } else {
            make.top.mas_equalTo(self.view.mas_top).offset(3);;
        }
        make.width.mas_equalTo(self.view);
        make.height.mas_equalTo(self.view.width).multipliedBy(1.5);
    }];
    JMSelf(ws);
    UIImageView *bkgImage = [[UIImageView alloc] initWithFrame:CGRectZero];
    bkgImage.image = [UIImage imageNamed:@"15"];
    bkgImage.contentMode = UIViewContentModeScaleAspectFill;
    bkgImage.clipsToBounds = YES;
    [self.bkg addSubview:bkgImage];
    self.bkgImage = bkgImage;
    [bkgImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(ws.bkg);
    }];
    
    NSString *s2 = [NSString stringWithFormat:@"|\n|\n%@", [self cp_deleteStr:_model.cp_author]];
    CGSize size2 = [s2 cp_height:14.0];
    CPLabel *author = [[CPLabel alloc] initWithFrame:CGRectMake(0, 0, size2.width, size2.height)];
    author.fSize = 14.0;
    author.CPText = s2;
    [self.bkg addSubview:author];
    [author mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(bkg).offset(-(size2.width*3+20));
        make.centerY.mas_equalTo(bkg);
        make.width.mas_equalTo(size2.width);
        make.height.mas_equalTo(size2.height+20);
    }];
    
    NSString *s1 = [NSString stringWithFormat:@"︽\n%@\n︾", [self cp_deleteStr:_model.cp_title]];
    CGSize h1 = [s1 cp_height:17.0];
    CPLabel *title = [[CPLabel alloc] initWithFrame:CGRectMake(0, 0, h1.width, h1.height)];
    title.fSize = 17.0;
    title.CPText = s1;
    [self.bkg addSubview:title];
    [title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(-h1.width);
        make.centerY.mas_equalTo(bkg);
        make.width.mas_equalTo(h1.width);
        make.height.mas_equalTo(h1.height+20);
    }];
    
    NSArray *strs = [self cp_content:_model.cp_content];
    NSString *s0 = [NSString stringWithFormat:@"%@\n，", [self cp_deleteStr:strs.lastObject]];
    CGSize size0 = [s0 cp_height:23.0];
    CPLabel *fContent = [[CPLabel alloc] initWithFrame:CGRectMake(0, 0, size0.width, size0.height)];
    fContent.CPText = s0;
    fContent.fSize = 23.0;
    [self.bkg addSubview:fContent];
    [fContent mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(bkg).offset(size0.width);
        make.centerY.mas_equalTo(bkg);
        make.width.mas_equalTo(size0.width);
        make.height.mas_equalTo(size0.height+20);
    }];
    
    NSString *sOne = [NSString stringWithFormat:@"%@\n。", [self cp_deleteStr:strs.firstObject]];
    CGSize sizeOne = [s0 cp_height:23.0];
    CPLabel *lContent = [[CPLabel alloc] initWithFrame:CGRectMake(0, 0, sizeOne.width, sizeOne.height)];
    lContent.fSize = 23.0;
    lContent.CPText = sOne;
    [self.bkg addSubview:lContent];
    [lContent mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(bkg).offset(size0.width+sizeOne.width+20);
        make.centerY.mas_equalTo(bkg);
        make.width.mas_equalTo(sizeOne.width);
        make.height.mas_equalTo(sizeOne.height+20);
    }];
}

#pragma mark -- 工具方法
- (NSString *)cp_deleteStr:(NSString *)string
{
    NSString *str = [string stringByReplacingOccurrencesOfString:@"，" withString:@""];
    NSString *str1 = [str stringByReplacingOccurrencesOfString:@"。" withString:@""];
    NSMutableArray *stringS = [NSMutableArray array];
    for (int i = 0; i < str1.length; i ++) {
        NSString *charS = [str1 substringWithRange:NSMakeRange(i, 1)];
        [stringS addObject:charS];
    }
    return [stringS componentsJoinedByString:@"\n"];
}

- (NSArray *)cp_content:(NSString *)cont
{
    NSArray *content = [cont componentsSeparatedByString:@"\n"];
    NSInteger count = content.count;
    NSString *last = @"";
    NSString *pre = @"";
    if ([content.lastObject length] < 3) {
        last = content[count-2];
        pre = content[count-3];
    }
    return @[last,pre];
}

@end
